package com.qen.w3school

import cn.hutool.core.lang.Console
import cn.hutool.core.util.StrUtil
import com.qen.epub.EpubOutPath
import com.qen.yanshen.UserAgent
import com.qen.zhihulanzhou.BaseUrl
import io.github.bonigarcia.wdm.WebDriverManager
import it.skrape.core.htmlDocument
import it.skrape.fetcher.BrowserFetcher
import it.skrape.fetcher.Method
import it.skrape.fetcher.response
import it.skrape.fetcher.skrape
import nl.siegmann.epublib.domain.Author
import nl.siegmann.epublib.domain.Book
import nl.siegmann.epublib.domain.Resource
import nl.siegmann.epublib.domain.Resources
import nl.siegmann.epublib.epub.EpubWriter
import nl.siegmann.epublib.service.MediatypeService
import org.openqa.selenium.By
import org.openqa.selenium.WebDriver
import org.openqa.selenium.chrome.ChromeDriver
import org.openqa.selenium.support.ui.ExpectedConditions
import org.openqa.selenium.support.ui.WebDriverWait
import org.python.modules.itertools.dropwhile
import java.io.File
import java.io.FileOutputStream
import java.time.Duration

const val W3SchoolUrl = "https://www.w3cschool.cn/architectroad/architectroad-zfxt23al.html"

const val W3SchoolPath = "/Users/a1/Documents/Git/Kotlin/Eleven/out/w3school/"

fun main() {
    val book = Book()
    book.metadata.addTitle("架构师")
    book.metadata.addAuthor(Author("MayBeTheGo"))
    val list = ArrayList<MutableMap<String, String>>()
    skrape(BrowserFetcher) {
        request {
            url = W3SchoolUrl
            headers = mutableMapOf(
                "accept" to "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
                "accept-language" to "zh-CN,zh;q=0.9,en;q=0.8",
                "cache-control" to "no-cache",
                "pragma" to "no-cache",
                "priority" to "u=0, i",
                "sec-ch-ua" to "\"Chromium\";v=\"130\", \"Google Chrome\";v=\"130\", \"Not?A_Brand\";v=\"99\"",
                "sec-ch-ua-mobile" to "?0",
                "sec-ch-ua-platform" to "\"macOS\"",
                "sec-fetch-dest" to "document",
                "sec-fetch-mode" to "navigate",
                "sec-fetch-site" to "same-origin",
                "sec-fetch-user" to "?1",
                "upgrade-insecure-requests" to "1"
            )
            cookies = mutableMapOf(

            )
            userAgent = UserAgent
            method = Method.GET
        }
        response {
            htmlDocument {
                "#nestable_handbook" {
                    val dd_item = findFirst(".dd-list").children
                    for (item in dd_item) {
                        val pTitle = item.findFirst("a").text
                        val ol = item.findFirst(".dd-list")
                        val subList = ol.children
                        Console.log("Ptitle:$pTitle")
                        for (sub in subList) {
                            val subTitle = sub.findFirst("a").text
                            val goUrl = sub.attribute("data-id")
                            Console.log("subTitle:$subTitle;GoUrl:$goUrl")

                            detail(
                                "$goUrl",
                                "$pTitle-$subTitle",
                                book
                            )

                            list.add(
                                mutableMapOf(
                                    "Ptitle" to pTitle,
                                    "subTitle" to subTitle,
                                    "GoUtl" to goUrl
                                )
                            )
                        }
                    }
                }
            }
        }
    }
    val outputFile = File("$W3SchoolPath/架构师.epub")
    val outputStream = FileOutputStream(outputFile)
    EpubWriter().write(book, outputStream)
    outputStream.close()
}

fun main1() {
    val book = Book()
    book.metadata.addTitle("架构师")
    book.metadata.addAuthor(Author("MayBeTheGo"))
    detail(
        "architectroad-distributed-id",
        "架构 细聊分布式ID生成方法",
        book
    )
    val outputFile = File("$W3SchoolPath/架构师.epub")
    val outputStream = FileOutputStream(outputFile)
    EpubWriter().write(book, outputStream)
    outputStream.close()
}

fun detail(goUrl: String, title: String, book: Book): String {
    val resources = Resources()
    try {
        skrape(BrowserFetcher) {
            request {
                url = "https://www.w3cschool.cn/architectroad/${goUrl}.html"
                headers = mutableMapOf(
                    "accept" to "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
                    "accept-language" to "zh-CN,zh;q=0.9,en;q=0.8",
                    "cache-control" to "no-cache",
                    "pragma" to "no-cache",
                    "priority" to "u=0, i",
                    "sec-ch-ua" to "\"Chromium\";v=\"130\", \"Google Chrome\";v=\"130\", \"Not?A_Brand\";v=\"99\"",
                    "sec-ch-ua-mobile" to "?0",
                    "sec-ch-ua-platform" to "\"macOS\"",
                    "sec-fetch-dest" to "document",
                    "sec-fetch-mode" to "navigate",
                    "sec-fetch-site" to "same-origin",
                    "sec-fetch-user" to "?1",
                    "upgrade-insecure-requests" to "1"
                )
                cookies = mutableMapOf(

                )
                userAgent = UserAgent
                method = Method.GET
                timeout = 60000
            }
            response {
                Console.log("请求:https://www.w3cschool.cn/architectroad/${goUrl}.html;成功失败${this.responseStatus.code}")
                if (this.responseStatus.code != 200) {
                    Thread.sleep(5000)
                    detail(goUrl, title, book)
                }
                htmlDocument {
                    ".content-bg" {
                        findFirst {
                            val contentImg = this.html.replace("<br>", "").replace("<hr>", "").replace("&nbsp;","")
                            val regex = """<img\s+[^>]*>""".toRegex()
                            val content = regex.replace(contentImg) { matchResult ->
                                val imgTag = matchResult.value
                                val attributes = imgTag.substringAfter("<img").substringBefore(">")
                                "<img$attributes></img>"
                            }
                            Console.log(content)

//                        <?xml version="1.0" encoding="UTF-8"?>
//                        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
                            val template = """
                            <!DOCTYPE html>
                           
                            <html xmlns="http://www.w3.org/1999/xhtml">
                            <head>
                                <title>${title}</title>
                            </head>
                            <body>
                                <h1>${title}</h1>
                                $content
                            </body>
                            </html>
                        """.trimIndent()
                            val chapter1Resource =
                                Resource(template.toByteArray(), "${System.currentTimeMillis()}.html")
                            chapter1Resource.mediaType = MediatypeService.XHTML
                            resources?.add(chapter1Resource)
                            book.addSection(title, chapter1Resource)
                        }
                    }
                }
            }
        }
        return ""
    } catch (e: Exception) {
        e.printStackTrace()
        return goUrl
    }
}

fun mainx() {
    WebDriverManager.chromedriver().setup()

    val driver: WebDriver = ChromeDriver()
    try {
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30))
        driver.get(W3SchoolUrl)

        val title = driver.title
        println(title)
        val wait = WebDriverWait(driver, Duration.ofSeconds(30))

        val nestable_handbook = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("nestable_handbook")))
        val dd_list = wait.until(ExpectedConditions.presenceOfElementLocated(By.className("dd-list")))
        val dd_item = dd_list.findElements(By.className("dd-item"))
        val dd_item_that = dd_item
        for (item in dd_item_that) {
            val pTitle = item.findElement(By.tagName("a")).text
            val ol = item.findElement(By.className("dd-list"))
            val subList = ol.findElements(By.className("dd-item"))
            val subList_that = subList
            Console.log("Ptitle:$pTitle")
            for (sub in subList_that) {
                val subTitle = sub.findElement(By.tagName("a")).text
                val goUrl = sub.getAttribute("data-id")
                Console.log("subTitle:$subTitle;GoUrl:$goUrl")
            }
        }
    } finally {
        driver.quit()

    }

}